import 'dart:async';

import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class PullToRefreshTest2Page extends StatefulWidget {
  @override
  _PullToRefreshTest2PageState createState() =>
      new _PullToRefreshTest2PageState();
}

class _PullToRefreshTest2PageState extends State<PullToRefreshTest2Page> {
  RefreshController _refreshController;
  List<Widget> data = _getDatas();

  static List<Widget> _getDatas() {
    List<Widget> data = [];
    for (int i = 0; i < 14; i++) {
      data.add(new Card(
        margin:
            new EdgeInsets.only(left: 10.0, right: 10.0, top: 5.0, bottom: 5.0),
        child: new Center(
          child: new Text('Data $i'),
        ),
      ));
    }
    return data;
  }

  void enterRefresh() {
    _refreshController.requestRefresh(true);
  }

  void _onOffsetCallback(bool isUp, double offset) {
    // if you want change some widgets state ,you should rewrite the callback
  }

  @override
  void initState() {
    // TODO: implement initState
    _getDatas();
    _refreshController = new RefreshController();
    super.initState();
  }

  Widget _headerCreate(BuildContext context, int mode) {
    return new ClassicIndicator(
      mode: mode,
      refreshingText: "",
      idleIcon: new Container(),
      idleText: "Load more...",
    );
  }

//  Widget _footerCreate(BuildContext context,int mode){
//    return new ClassicIndicator(mode: mode);
//  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("刷新")),
        body: new SmartRefresher(
            enablePullDown: true,
            enablePullUp: true,
            controller: _refreshController,
            onRefresh: _onRefresh,
            onOffsetChange: _onOffsetCallback,
            child: new ListView.builder(
              reverse: true,
              itemExtent: 100.0,
              itemCount: data.length,
              itemBuilder: (context, index) => new Item(index),
            )));
  }

  void _onRefresh(bool up) {
    if (up) {
      // 下拉刷新
      if (data.length >= 15) {
        _refreshController.sendBack(true, RefreshStatus.canRefresh);
        return;
      }
      Future.delayed(const Duration(milliseconds: 2009)).then((val) {
        setState(() {
          data.add(new Card(
            margin: new EdgeInsets.only(
                left: 10.0, right: 10.0, top: 5.0, bottom: 5.0),
            child: new Center(
              child: new Text('Data ${data.length + 1}'),
            ),
          ));
        });

        _refreshController
            .scrollTo(_refreshController.scrollController.offset + 50.0);
        _refreshController.sendBack(true, RefreshStatus.idle);
      });
    } else {
      // 上拉加载
      new Future.delayed(const Duration(milliseconds: 2009)).then((val) {
        setState(() {
          data.add(new Card(
            margin: new EdgeInsets.only(
                left: 10.0, right: 10.0, top: 5.0, bottom: 5.0),
            child: new Center(
              child: new Text('Data ${data.length + 1}'),
            ),
          ));
        });
        if(data.length >= 15) {
          _refreshController.sendBack(false, RefreshStatus.noMore);
        } else {
          _refreshController.sendBack(false, RefreshStatus.idle);
        }
      });
    }
  }
}

class Item extends StatelessWidget {
  final int index;

  Item(this.index);

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.only(left: 10.0, right: 10.0, top: 5.0, bottom: 5.0),
      child: Center(
        child: Text('Data ${index}'),
      ),
    );
  }
}
